var app;
app = new Vue({
    el: '#app',
    data: {
        form: {
            RoomID: 0,
            FullName: '',
            RealName: '',
            PhoneNumber: ''
        },
        list: [],
        HeJiCost: 0,
        paymentmethods: [],
        allchecked: false,
        calMonth: '',
        NeedChargeWechatAll: false
    },
    methods: {
        get_data: function() {
            var that = this;
            that.get_fee_data();
        },
        open_history: function() {
            var that = this;
            ns.openWin('chargeroomfeehistory_frm', '缴费记录', {
                id: that.form.RoomID
            });
        },
        do_pay: function() {
            var that = this;
            that.calculateTotalFee();
            var idlist = that.get_pay_idlist();
            if (that.HeJiCost == 0) {
                api.toast({
                    msg: '收费金额不能为0',
                    duration: 2000,
                    location: 'bottom'
                });
                return
            }
            ns.openWin('shoufeipay_frm', '收费确认', {
                id: that.HeJiCost,
                idlist: JSON.stringify(idlist),
                name: that.CurrentProjectName,
            });
        },
        get_fee_data: function() {
            var that = this;
            ns.post({
                action: 'getroomfeebyfromroomid',
                FromRoomID: that.form.RoomID,
            }, function(succeed, data, err) {
                if (succeed) {
                    that.form = data.dataform;
                    that.list = data.list;
                    that.calculateTotalFee();
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });

                }
            });
        },
        do_reduce_month: function(item) {
            var that = this;
            if (!item.IsCanSelect) {
                return;
            }
            item.Selected = true;
            if (item.MonthAddCount > item.MinMonthAddNumber) {
                item.MonthAddCount--;
            }
            item.CalMonthNumber = item.MonthAddNumber * item.MonthAddCount;
            that.get_roomfee_bytime(item);
        },
        do_add_month: function(item) {
            var that = this;
            if (!item.IsCanSelect) {
                return;
            }
            item.Selected = true;
            if (item.MaxMonthAddNumber > 0 && item.CalMonthNumber >= item.MaxMonthAddNumber) {
                item.CalMonthNumber = item.MaxMonthAddNumber;
            } else {
                item.MonthAddCount++;
                item.CalMonthNumber = item.MonthAddNumber * item.MonthAddCount;
                if (item.MaxMonthAddNumber > 0 && item.CalMonthNumber >= item.MaxMonthAddNumber) {
                    item.CalMonthNumber = item.MaxMonthAddNumber;
                }
            }
            that.get_roomfee_bytime(item);
        },
        choose_fee: function(item) {
            var that = this;
            if (!item.IsCanSelect || item.NeedChargeWechatAll) {
                return;
            }
            item.Selected = !item.Selected;
            that.calculateTotalFee();
        },
        calculateTotalFee: function() {
            var that = this;
            total_fee = 0;
            for (var i = 0; i < that.list.length; i++) {
                var item1 = that.list[i];
                //绑定缴费
                if (item1.NeedChargeWechatAll && item1.IsCanSelect) {
                    total_fee += item1.TotalCost;
                    for (var j = 0; j < item1.childfee.length; j++) {
                        var item2 = item1.childfee[j];
                        total_fee += item2.TotalCost;
                    }
                }
                //单独缴费
                else if (!item1.NeedChargeWechatAll && item1.IsCanSelect && item1.Selected) {
                    total_fee += item1.TotalCost;
                    for (var j = 0; j < item1.childfee.length; j++) {
                        var item2 = item1.childfee[j];
                        total_fee += item2.TotalCost;
                    }
                }
                if (item1.NeedChargeWechatAll) {
                    that.NeedChargeWechatAll = true;
                }
            }
            that.HeJiCost = total_fee;
        },
        get_roomfee_bytime: function(item) {
            var that = this;
            ns.post({
                action: 'getroomfeebyid',
                id: item.ID,
                endtime: item.EndTime,
                CalMonthNumber: item.CalMonthNumber,
                ChargeID: item.ChargeID,
                StartTime: item.StartTime,
                AutoToMonthEnd: item.AutoToMonthEnd ? 1 : 0
            }, function(succeed, data, err) {
                if (succeed) {
                    item.TotalCost = data.TotalCost;
                    item.EndTime = data.EndTime;
                    that.calculateTotalFee();
                }
            });
        },
        get_pay_idlist: function() {
            var that = this;
            var idlist = [];
            for (var i = 0; i < that.list.length; i++) {
                var item1 = that.list[i];
                //绑定缴费
                if (item1.NeedChargeWechatAll && item1.IsCanSelect) {
                    idlist.push({
                        ID: item1.ID,
                        EndTime: item1.EndTime.replace('T', ' ')
                    });
                    for (var j = 0; j < item1.childfee.length; j++) {
                        var item2 = item1.childfee[j];
                        idlist.push({
                            ID: item2.ID,
                            EndTime: item2.EndTime.replace('T', ' ')
                        });
                    }
                }
                //单独缴费
                else if (!item1.NeedChargeWechatAll && item1.Selected && item1.IsCanSelect) {
                    idlist.push({
                        ID: item1.ID,
                        EndTime: item1.EndTime.replace('T', ' ')
                    });
                    for (var j = 0; j < item1.childfee.length; j++) {
                        var item2 = item1.childfee[j];
                        idlist.push({
                            ID: item2.ID,
                            EndTime: item2.EndTime.replace('T', ' ')
                        });
                    }
                }
            }
            return idlist;
        },
        select_all: function() {
            var that = this;
            that.allchecked = !that.allchecked;
            for (var i = 0; i < that.list.length; i++) {
                var current = that.list[i];
                if (!current.IsCanSelect) {
                    current.Selected = false;
                    continue;
                }
                current.Selected = that.allchecked;
            }
            that.calculateTotalFee();
        }
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.form.RoomID = api.pageParam.id;
    app.get_data();
}
